const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const check = require('../../utils/check.js');
const rules = {
    name: '姓名',
    mobile: '手机',
    idCard: '身份证',
    bankAccount: '银行卡号',
    openBank: '开户行',
    workUnit: '单位名称',
    typeName: '汇款项目',
    typeNote: '备注',
}

Page({
    data: {
        name: '',
        mobile: '',
        bankAccount: "",
        idCard: "",
        openBank: "",
        workUnit: "",
        types: [
            '学术活动',
            '期刊编辑部',
            '科普国际交流'
        ],
        showPicker: false,
        typeName: '',//汇款项目名称
        type: '',//汇款项目
        id: '',
        result: [],
        //备注
        remarksList: ['轮机专业委员会', '电气和自动化专业委员会', '设计专业委员会', '流体力学专业委员会',
            '标准与规范专业委员会', '结构力学专业委员会', '海洋工程专业委员会', '建造工艺和材料专业委员会', '信息技术专业委员会', '辅机专业委员会',
            '管理科学和船史专业委员会', '学术工作委员会', '大型综合学术年会', '工博会科技论坛', '青年科技论坛', '科技评价',
            '课题研究', '辛一心理事会及评选', '优秀论文评选', '其他'],
        typeNote: '',//备注
        showPicker_2: false,
        showPicker_3: false,
        noRemarksList: [],
        //提示信息
        typeNoteError: "",
        nameError: '',
        bankAccountError: "",
        idCardError: "",
        openBankError: "",
        workUnitError: "",
        typeNameError: '',
        phoneError: '',
    },

    onShareAppMessage() {
        return {};
    },
    onChangeGroup(event) {
        this.setData({
            result: event.detail
        })
    },
    //电话校验格式
    phoneCheck(e) {
        let results = check.isValidPhone(e.detail)
        this.setData({
            phoneError: results
        })
    },
    onLoad(options) {
        const that = this;
        if (Object.keys(options).length) {
            util.request(api.RemittanceDetail, {id: options.id}, 'get').then(function (res) {
                if (res.errno === 200) {
                    const info = res.data;
                    that.setData({
                        id: options.id,
                        name: info.name,
                        mobile: info.mobile,
                        bankAccount: info.bankAccount,
                        idCard: info.idCard,
                        workUnit: info.workUnit,
                        typeName: info.typeDesc,
                        openBank: info.openBank,
                        type: info.type,
                        typeNote: info.typeNote,
                    });
                }
            });
        }
    },

    onSubmit() {
        const {id, idCard, name, typeName, type, bankAccount, openBank, workUnit, mobile, typeNote} = this.data;
        let infoCreate = {typeName, id, idCard, name, type, bankAccount, openBank, workUnit, mobile, typeNote}
        let infoApply = {}
        if (type == '0') {
            infoApply = {
                typeName,
                idCard,
                name,
                bankAccount,
                openBank,
                workUnit,
                mobile,
                typeNote
            }
        } else {
            infoApply = {
                typeName,
                idCard,
                name,
                bankAccount,
                openBank,
                workUnit,
                mobile,
            }
        }
        let hasError = false
        let keys = Object.keys(infoApply);
        for (let i = 0; i < keys.length; i++) {
            let message = this.validate(null, keys[i])
            if (message) {
                hasError = true;
            }
        }
        if (hasError) {
            wx.showToast({
                title: '字段填写不完整',
                icon: 'error',
                duration: 2000
            })
            return;
        }
        if (!id) {// 新增
            delete infoCreate.id;
            this.requestApi(infoCreate);
        } else {// 编辑
            util.request(api.RemittanceModify, infoCreate, 'post')
                .then(res => {
                    if (res.errno === 200) {
                        wx.showModal({
                            content: '您的信息已提交，请耐心等待，注意查收！',
                            showCancel: false,//是否显示取消按钮
                            confirmText: "确定",//默认是“确定”
                            confirmColor: 'rgb(31, 103, 220)',//确定文字的颜色
                            success: function (res) {
                                if (res.confirm) {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                }
                            },
                        })
                    } else {
                        wx.showToast({
                            title: res.errmsg,
                            icon: 'error',
                            duration: 2000
                        })
                    }
                })
        }
    },
    validate(e, objKey) {
        let {key} = (e && e.target.dataset) || {}
        let curKey = objKey || key;
        if (!rules.hasOwnProperty(curKey)) return ''
        let curVal = this.data[curKey];
        let message = '';
        if (!curVal) {
            this.setData({
                [curKey + 'Error']: rules[curKey] + '不可为空',
            })
            message = rules[curKey] + '不可为空';
        } else {
            this.setData({
                [curKey + 'Error']: '',
            })
            message = ''
        }
        //进行额外的格式校验
        switch (objKey) {
            case 'mobile':
                message = check.isValidPhone(curVal)
                this.setData({
                    phoneError: message
                })
                break;
        }
        return message;
    },
    requestApi(infoCreate) {
        util.request(api.RemittanceCreate, infoCreate, 'post')
            .then(res => {
                if (res.errno === 200) {
                    wx.showModal({
                        content: '您的信息已提交，请耐心等待，注意查收！',
                        showCancel: false,//是否显示取消按钮
                        confirmText: "确定",//默认是“确定”
                        confirmColor: 'rgb(31, 103, 220)',//确定文字的颜色
                        success: function (res) {
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                })
                            }
                        },
                    })
                } else {
                    wx.showToast({
                        title: res.errmsg,//提示文字
                        duration: 2000,//显示时长
                        mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
                        icon: 'error', //图标，支持"success"、"loading"
                    })
                }
            })
    },
    showPicker() {
        this.setData({
            showPicker: true,
        })
    },
    onConfirm(e) {
        //备注内容根据汇款项目是否为空
        if (e.detail.index != 0) {
            this.setData({
                typeNote: '',
                typeNameError: ''
            })
        }
        this.setData({
            typeName: e.detail.value,
            type: e.detail.index,
            showPicker: false,
            typeNameError: ''
        })
    },
    onCancel() {
        this.setData({
            showPicker: false,
        })
    },
    showPicker_2() {
        this.setData({
            showPicker_2: true,
        })
    },
    onConfirm_2(e) {
        this.setData({
            typeNote: e.detail.value,
            showPicker_2: false,
            typeNoteError: ''
        })
    },
    onCancel_2() {
        this.setData({
            showPicker_2: false,
        })
    },
    showPicker_3() {
        this.setData({
            showPicker_2: true,
        })
    },
    onConfirm_3(e) {
        this.setData({
            typeNote: "",//字符串型
            showPicker_2: false,
            typeNoteError: ''
        })
    },
    onCancel_3() {
        this.setData({
            showPicker_2: false,
        })
    }
});
